﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using nrtech.bot.compras.bll;

namespace nrtech.bot.compras.ui
{
    public partial class fMenu : Form
    {
        private bool iniciado = true;
        public fMenu()
        {
            try
            {
                InitializeComponent();
                Config.lerConfigGeral();

                tbURL.Text = Config.url;
                tbTags.Text = Config.tags;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro ao carregar aplicativo: " + ex.Message);
            }
        }

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String tagsAntigas = Config.tags;
            try
            {
                tmMonitor.Enabled = false;
                Config.tags = tbTags.Text;
                Constants.defineNavegador(webBrowser1);
                startMonitor.monitorar("M", tbTags.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                tmMonitor.Enabled = true;
                Config.tags = tagsAntigas;
            }
        }

        private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            progressoNaveg.Maximum = Convert.ToInt32(e.MaximumProgress);
            progressoNaveg.Value = Convert.ToInt32(e.CurrentProgress < 0 ? 0 : 
                e.CurrentProgress > e.MaximumProgress ? e.MaximumProgress : e.CurrentProgress);
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            statusNaveg.Text = "Abrindo " + e.Url.ToString();
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            statusNaveg.Text = "";
            progressoNaveg.Value = 0;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            fConfig config = new fConfig();
            config.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void tmMonitor_Tick(object sender, EventArgs e)
        {
            String horaAtual = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
            if (horaAtual.Equals(Config.horaMonitor) || (horaAtual + ":00").Equals(Config.horaMonitor) || !iniciado)
            {
                iniciado = true;
                try
                {
                    tmMonitor.Enabled = false;
                    //Config.lerConfig();
                    Constants.defineNavegador(webBrowser1);
                    startMonitor.monitorar("A", null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    tmMonitor.Enabled = true;
                }
            }
        }

    }
}
